package com.算法.Java基础;


import java.io.*;
import java.util.ArrayList;
import java.util.Random;

/**
 * <p>
 *       请你用IO流技术，把一个文本文件放到你的本地磁盘或者项目路径下，里面随意放一些同学名字，实现一个随机点名效果
 * </p>
 *
 * @author zh
 * @date 2024-07-10 10:18
 */
public class IO操作 {

    public static void main(String[] args) throws IOException {

        // 1. 读取文本文件中的同学名字
        String filePath = "D:\\Program Files (x86)\\Java\\idea\\算法\\src\\com\\算法\\Java基础\\file\\file.txt";

        // 2.
        BufferedReader sb = new BufferedReader(new FileReader(filePath));
        String line;
        ArrayList<String> lists = new ArrayList<String>();
        while ((line = sb.readLine())!= null) {
            lists.add(line);
        }
        int nextInt = new Random().nextInt(3);
        System.out.println("随机点名："+lists.get(nextInt));

        test();
    }


    public static void test() throws IOException {
        // 1. 读取文本文件中的同学名字
        String filePath = "D:\\Program Files (x86)\\Java\\idea\\算法\\src\\com\\算法\\Java基础\\file\\file.txt";

        // 2.
        BufferedReader sb = new BufferedReader(new FileReader(filePath));
        String line;
        String[] str = {"","","",""};
        int i = 0;
        while ((line = sb.readLine())!= null) {
            str[i] = line;
            i++;
        }
        int nextInt = new Random().nextInt(3);
        System.out.println("随机点名："+str[nextInt]);
    }
}
